/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import path from 'path';

import { expect } from '@jest/globals';

import { runHvigorAsync } from '../../common/common';
import { commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1,
    [ModuleTemplateType.SharedLibrary]: 1,
  }
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();

  const entry = project.getModule('entry');
  const staticLibrary = project.getModule('staticLibrary');
  const shareLibrary = project.getModule('shareLibrary');

  const staticCommands: string[] =
      '--mode module -p product=default -p module=staticLibrary@default assembleHar --analyze=normal --parallel --incremental --daemon'.split(' ');

  await commonJson5Operation(path.resolve(staticLibrary.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0001';
  });

  const result1 = await runHvigorAsync(projectRootPath, staticCommands);
  record.addStep('首次构建，PreBuild为Finished', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :staticLibrary:default@PreBuild');
  expect(result1.stderr).toContain('The version number of the \'staticLibrary\' module does not comply with the SemVer specification');

  const shareCommands: string[] =
      '--mode module -p product=default -p module=shareLibrary@default assembleHsp --analyze=normal --parallel --incremental --daemon'.split(' ');

  await commonJson5Operation(path.resolve(shareLibrary.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0001';
  });

  const result2 = await runHvigorAsync(projectRootPath, shareCommands);
  record.addStep('首次构建，PreBuild为Finished', result2);
  expect(result2.stdout).toContain('BUILD SUCCESSFUL');
  expect(result2.stdout).toContain('Finished :shareLibrary:default@PreBuild');
  expect(result2.stderr).toContain('The version number of the \'shareLibrary\' module does not comply with the SemVer specification');

  const hapCommands: string[] =
      '--mode module -p product=default assembleHap --analyze=normal --parallel --incremental --daemon'.split(' ');

  await commonJson5Operation(path.resolve(entry.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0001';
  });

  const result3 = await runHvigorAsync(projectRootPath, hapCommands);
  record.addStep('首次构建，PreBuild为Finished', result3);
  expect(result3.stdout).toContain('BUILD SUCCESSFUL');
  expect(result3.stdout).toContain('Finished :entry:default@PreBuild');
  expect(result3.stderr).toContain('The version number of the \'entry\' module does not comply with the SemVer specification');

  const appCommands: string[] =
      '--mode project -p product=default assembleApp --analyze=normal --parallel --incremental --daemon'.split(' ');

  await commonJson5Operation(path.resolve(entry.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0002';
  });

  const result4 = await runHvigorAsync(projectRootPath, appCommands);
  record.addStep('首次构建，PreBuild为Finished', result4);
  expect(result4.stdout).toContain('BUILD SUCCESSFUL');
  expect(result4.stdout).toContain('Finished :entry:default@PreBuild');
  expect(result4.stderr).toContain('The version number of the \'entry\' module does not comply with the SemVer specification');

  await commonJson5Operation(path.resolve(entry.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0';
  });

  await commonJson5Operation(path.resolve(shareLibrary.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0002';
  });

  const result5 = await runHvigorAsync(projectRootPath, appCommands);
  record.addStep('首次构建，PreBuild为Finished', result5);
  expect(result5.stdout).toContain('BUILD SUCCESSFUL');
  expect(result5.stdout).toContain('Finished :shareLibrary:default@PreBuild');
  expect(result5.stderr).toContain('The version number of the \'shareLibrary\' module does not comply with the SemVer specification');

  await commonJson5Operation(path.resolve(staticLibrary.moduleRootPath(), 'oh-package.json5'), async (json5) => {
    json5.version = '1.0.0';
    json5.name = '@huawei/nameCheck/har';
  });

  const result6 = await runHvigorAsync(projectRootPath, staticCommands);
  record.addStep('首次构建，PreBuild为Finished', result6);
  expect(result6.stdout).toContain('BUILD SUCCESSFUL');
  expect(result6.stdout).toContain('Finished :staticLibrary:default@PreBuild');
  expect(result6.stderr).toContain('The name of the \'@huawei/nameCheck/har\' module does not comply with the regular expression: /^(@(?![0-9-_])[a-z0-9-_]+(?<![-_])\\/)?(?![0-9-_.])[a-z0-9-_.]+(?<![-_.])$/');

};

TestCaseBuilder.builder()
  .caseName('SemverRegularCheck')
  .description('RegularCheck代码合并测试用例验证')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L2).build())
  .testFun(testfun)
  .load();
